-- region *.lua
-- Date
-- 此文件由[BabeLua]插件自动生成

-- 菜单样式
MenuChooseDlgStyle =
{
    CellWidth = 150;
    CellHeight = 50;
    BgResID = ResID(7,116,
    120);
    DivResID = ResID(7,1001,
    13);
    MouseEnterCellBg = ResID(7,116,
    121);
    ShowMode = 0;
    FontSize = 22;
}

function MenuChooseDlgStyle:new()
    local style = New(MenuChooseDlgStyle);
    style.CellWidth = 180;
    style.CellHeight = 60;
    style.BgResID = ResID(7, 116, 120);
    style.DivResID = ResID(7, 1001, 13);
    style.MouseEnterCellBg = ResID(7, 1001, 13);
    style.MouseEnterCellBg = ResID(7, 116, 121);
    style.FontSize = 22;
    style.ShowMode = MenuChooseDlg.SHOWMODEL_NONE;
    return style;
end

MenuChooseDlg = Dialog:DeriveClass("MenuChooseDlg", "MenuChooseDlg.prefab", 99,
{
    _MenuIndex = 1;
    _CellVec = Vector:new();

    _TotalHeight = 0;
    _HandlerCaller = nil;
    _CommonHandler = nil;
    _MenuBox = nil;
    _MenuBoxRectTransform = nil;
    _MenuStyle = nil;

    -- 左边的小箭头
    _LeftArrows = nil;
    _RightArrows = nil;

    -- 显示误差
    _ShowDis = 0;
    _ShowDisErr = 0;

    SHOWMODE_NONE = 0;
    SHOWMODE_LEFT = 1;
    SHOWMODE_RIGHT = 2;
    SHOWMODE_TOP = 3;
    SHOWMODE_BOTTOM = 4;

    _LeftDis = 35;
    _RightDis = -35;
    _CellRes;
} )


-- 使用方法 DlgMgr:FetchDlg(MenuChooseDlg,{{id = 1 text="副族长" args = {}},{id ="长老",text=self.GiveJob2Handler}},CommonHandler,MenuChooseDlgStyle);
function MenuChooseDlg:OnInit(...)
    self._CellRes = GuiUtil.LoadResObject("MenuChooseCell.prefab", 99);
    local args = { ...};
    self._CellVec = Vector:new();
    self._MenuIndex = 0;
    args = args[1];
    self._HandlerCaller = args[1];
    local table = args[2];
    self._CommonHandler = args[3];
    self._MenuStyle = args[4];
    if self._MenuStyle == nil then
        self._MenuStyle = MenuChooseDlgStyle:new();
    end

    self._TotalHeight = 0;
    self._MenuBox = GuiUtil.GetYImage(self:GetWindow().Root.gameObject, "");
    self._MenuBoxRectTransform = self._MenuBox.gameObject:GetComponent(RectTransform);

    if self._MenuStyle ~= nil then
        if self._MenuStyle.ShowMode == MenuChooseDlg.SHODEMODE_NONE then
            self._ShowDis = self._MenuStyle.CellWidth * 0.1;
            self._ShowDisErr = 0;
        elseif self._MenuStyle.ShowMode == MenuChooseDlg.SHOWMODE_LEFT then
            self._ShowDis = self._LeftDis;
            self._ShowDisErr = -5;
            self._MenuBoxRectTransform.localScale = Vector3(-1, 1, 1);
        elseif self._MenuStyle.ShowMode == MenuChooseDlg.SHOWMODE_RIGHT then
            self._ShowDis = self._RightDis;
            self._ShowDisErr = 5;
            self._MenuBoxRectTransform.localScale = Vector3(1, 1, 1);
        else
            self._ShowDis = 0;
            self._ShowDisErr = 0;
        end
    else
        self._ShowDis = self._MenuStyle.CellWidth * 0.1;
        self._ShowDisErr = 0;
    end

    for index = 1, #table do
        local key = table[index].id;
        local value = table[index].text;
        local args = table[index].args;
        self:AddMenuCell(key, value, args);
        if self._MenuIndex < #table then
            self:AddDiv();
        end;
    end

    self._MenuBoxRectTransform.sizeDelta = Vector2(self._MenuStyle.CellWidth, self._TotalHeight);
    self:SetClickHitHandler(MenuChooseDlg.HandleClickHit);
end

function MenuChooseDlg:HandleClickHit(objHit, hitMe)
    --家族那块
    local familyMemberListPanel = DlgMgr:GetDlg(FamilyMemberListPanel);
    if familyMemberListPanel ~= nil then
        local memberList  = familyMemberListPanel._MemberList;
        if memberList.gameObject.activeSelf then
            if objHit.transform:IsChildOf(memberList.gameObject.transform) then
                local selectCell = familyMemberListPanel:GetSelectMemberCell();
                if selectCell and objHit.transform:IsChildOf(selectCell.gameObject.transform) then
                    return;
                end
            end
        end
    end

    if objHit ~= nil and objHit.transform:IsChildOf(self:GetWindow().Root.transform) then
        return;
    end
    if not hitMe then
        self:Close();
    end
end

function MenuChooseDlg:SetPosition(position)
    self._MenuBoxRectTransform.anchoredPosition3D = position;
end

-- 设置安全的位置，保证不超出屏幕
-- worldPosition：世界坐标
-- pivot: pivot点，可选
-- 目前只处理不超出底框
function MenuChooseDlg:SetSafePosition(worldPosition, pivot)
    local rtt = self._MenuBoxRectTransform;
    if pivot then
        rtt.pivot = pivot;
    end
    rtt.anchorMin = Vector2(0.5, 0.5);
    rtt.anchorMax = Vector2(0.5, 0.5);
    local sceneWidth = LuaEntry.windowMgr.sceneWidth;
    local sceneHeight = LuaEntry.windowMgr.sceneHeight;
    local size = rtt.sizeDelta;
    local targetPositon = worldPosition / LuaEntry.windowMgr.scaleFactor;
    if sceneHeight * rtt.anchorMin.y < - targetPositon.y + size.y * rtt.pivot.y then
        targetPositon.y = -(sceneHeight * rtt.anchorMin.y - size.y * rtt.pivot.y);
    end

    if sceneWidth * rtt.anchorMin.x < targetPositon.x + size.x * (1 - rtt.pivot.x) then
        targetPositon.x = sceneWidth * rtt.anchorMin.x - size.x * (1 - rtt.pivot.x);
    end
    rtt.anchoredPosition = targetPositon;
end

function MenuChooseDlg:AddMenuCell(cellID, cellName, cellArgs)
    local obj = ObjectPool.Instantiate(self._CellRes);
    local cellTransform = obj:GetComponent(RectTransform);
    cellTransform.anchorMin = Vector2(0.5, 1);
    cellTransform.anchorMax = Vector2(0.5, 1);
    cellTransform.pivot = Vector2(0.5, 1);
    local posY =(-1) *((self._MenuIndex) * self._MenuStyle.CellHeight);
    obj:GetComponent(Transform):SetParent(self._MenuBox.gameObject:GetComponent(Transform));
    cellTransform.sizeDelta = Vector2(self._MenuStyle.CellWidth - math.abs(self._ShowDis), self._MenuStyle.CellHeight);
    cellTransform.transform.localScale = Vector3(1, 1, 1);
    cellTransform.anchoredPosition3D = Vector3(self._ShowDisErr / 2, posY, 0);

    local btn = GuiUtil.GetYButton(obj, "Button");
    btn.text = cellName;
    btn.label.fontSize = self._MenuStyle.FontSize;
    btn:SetClickHandler(self, self.OnClickCellHandler, { cellID, cellArgs });
    self._CellVec:PushBack(btn);
    self._MenuIndex = self._MenuIndex + 1;
    self._TotalHeight = self._TotalHeight + self._MenuStyle.CellHeight;
end

function MenuChooseDlg:AddDiv()
    local div = self:AddImage();
    local divTransform = div.gameObject:GetComponent(RectTransform);
    if divTransform == nil then
        divTransform = div:AddComponent(RectTransform);
    end
    divTransform.anchorMin = Vector2(0.5, 1);
    divTransform.anchorMax = Vector2(0.5, 1);
    divTransform.pivot = Vector2(0.5, 1);
    local posY =(-1) *((self._MenuIndex) *(self._MenuStyle.CellHeight));
    div.gameObject:GetComponent(Transform):SetParent(self._MenuBox.gameObject:GetComponent(Transform));
    divTransform.sizeDelta = Vector2(self._MenuStyle.CellWidth - math.abs(self._ShowDis), 3);
    divTransform.transform.localScale = Vector3(1, 1, 1);
    divTransform.anchoredPosition3D = Vector3(0, posY, 0);
    div.imageID = self._MenuStyle.DivResID;
end;

function MenuChooseDlg:OnClickCellHandler(_, args)
    local cellID = args[1];
    local cellArgs = args[2]
    if self._HandlerCaller ~= nil and self._CommonHandler ~= nil then
        self._CommonHandler(self._HandlerCaller, cellID, cellArgs);
    end
end

function MenuChooseDlg:OnClose()
    for i = 1, self._CellVec:Size() do
        self._CellVec:At(i):ClearLuaHandler();
    end
end
-- endregion
